/*

Stickman Adventure - a game created by Daco & his cousin

Copyright (C) 2011-2012  DacoTaco & Wouterke

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


*/
#ifndef _Data_Collection_H
#define _Data_Collection_H

#include "global.h"
#include <map>
/*#include <SDL_image.h>
#include <SDL_mixer.h>*/

using namespace std;
/*
template classen voor data bij te houden die op meerdere plaatsen gebruikt wordt 
enkel support voor SDL_Surface* en een Mix_Chunk* 

*/

template <class N>
struct amount_data_pointers{
	unsigned short amount;
	N* data;	
};

template <class T>
class Data_Collection{
public:

	
T* addData(std::string image_name){
	if(image_name.size() <= 0)
		return NULL;
	map <std::string, amount_data_pointers<T>> :: const_iterator it= images.find(image_name);
	amount_data_pointers<T> a_i;
	if(it==images.end())
	{
		a_i.amount = 1;
		a_i.data=NULL;
		a_i.data=load(image_name,a_i.data);
		if(a_i.data==NULL) return NULL;
		images.insert(pair<std::string,amount_data_pointers<T>>(image_name,a_i));
	}
	else  {
		a_i = it->second;
		images[image_name].amount++;	
	}
	
	return a_i.data;
}

	
char deleteData(std::string image_name)
{
	if(image_name.size() <= 0 )
		return NULL;
	map <std::string, amount_data_pointers<T>> :: const_iterator it= images.find(image_name);		
	if(it==images.end()) return -1;
	//amount_data_pointers<T> a_i=it->second;
	images[image_name].amount--;
	if(images[image_name].amount==0 && images[image_name].data != NULL){
		images.erase(it);
	}
	return 0;
}

T* getData(std::string data){
	return NULL;
}


private:
	std::map<std::string,amount_data_pointers<T>> images;

	/*
	een loadfunctie voor elke type van object die de template kan gebruiken
	dus op het moment enkel een SDL_Surface* en een Mix_Chunk*
	*/

	T*  load(std::string image_name, SDL_Surface* surface){

		return load_image(image_name,0x0000ffff);

	};


	T* load(std::string image_name, Mix_Chunk* sound){
		return Mix_LoadWAV(image_name.c_str());

	};
};

extern Data_Collection<SDL_Surface> image_collection;
extern Data_Collection<Mix_Chunk> sound_collection;
#endif